const mongoose = require('mongoose')

module.exports = function(app){
  const options = this.options
  mongoose.connect(`mongodb://${options.db.host}:${options.db.port}/${options.db.name}`, {
    user: options.db.user,
    pass: options.db.pwd
  })

  const link = mongoose.connection

  link.on('connected', () => {
    this.db.link = link
    const schema = this.createdSchema(link)
    Object.assign(this.db.schema, schema)
    options.init(app)
  })

  link.on('error', err => {
    this.db = err
    options.init(app)
    console.error(err)
  });
}
